package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.core.entity.jz.CShoppingCart;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.enums.BusinessScopeEnums;
import com.sxbbc.common.core.enums.BusinessTypeEnums;
import com.sxbbc.common.core.mapper.jz.CShoppingCartMapper;
import com.sxbbc.common.core.service.ICShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.IProSkuDetailService;
import com.sxbbc.common.core.service.IProcMainService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户商品购物车 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-06-03
 */
@Service
public class CShoppingCartServiceImpl extends ServiceImpl<CShoppingCartMapper, CShoppingCart> implements ICShoppingCartService {

    @Autowired
    private CShoppingCartMapper shoppingCartMapper;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private IProSkuDetailService proSkuDetailService;

    @Override
    public List<CShoppingCart> queryMyShoppingCart(Integer cid) {
        List<CShoppingCart> list = shoppingCartMapper.queryShoppingGroupByBid(cid, BusinessScopeEnums.ONLINE.getId());
        for (CShoppingCart s : list) {
            s.setCid(cid);
            List<CShoppingCart> shopList = shoppingCartMapper.queryShoppingByCid(s,BusinessScopeEnums.ONLINE.getId());
            shopList.forEach(x -> x.setTypeName(BusinessTypeEnums.getName(x.getBType())));
            s.setShopList(shopList);
        }
        return list;
    }

    @Override
    public List<CShoppingCart> queryMyShoppingCartByNearBussiness(Integer cid,Integer bid) {
        return shoppingCartMapper.queryShoppingByBid(cid, bid, BusinessScopeEnums.OFFLINE.getId());
    }

    @Override
    public boolean addShoppingCart(CShoppingCart cart) {
        //判断是否存在购物车内
        boolean save = false;
        ProcMain procMain = procMainService.selectOneByProCode(cart.getProCode());
        if (procMain == null) {
            return save;
        }
        //获取商品详情
        ProSkuDetail skuDetail = new ProSkuDetail();
        if (StringUtils.isNotBlank(cart.getSku())) {
            QueryWrapper<ProSkuDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("pro_code",cart.getProCode()).eq("spec_name",cart.getSku());
            skuDetail = proSkuDetailService.getOne(wrapper);
        }else if(cart.getSkuId()!=null){
            skuDetail =proSkuDetailService.getById(cart.getSkuId());
        }
        //查询购物车中是否存在该商品
        QueryWrapper<CShoppingCart> shopWrapper = new QueryWrapper<>();
        shopWrapper.eq("cid",cart.getCid()).eq("pro_code", cart.getProCode()).eq("sku_id",skuDetail.getId());
        CShoppingCart shoppingCart = super.getOne(shopWrapper);
        if (shoppingCart == null) {
            cart.setSkuId(skuDetail.getId());
            cart.setProId(procMain.getId());
            cart.setProUrl(procMain.getProUrl());
            cart.setProName(procMain.getProName());
//            type原先存储的是产品类型，金种农牧项目存储的是线上线下
           //cart.setType(procMain.getProType());
            save = super.save(cart);
        }else{
            if (cart.getQty() > 0 && cart.getQty() + shoppingCart.getQty() > shoppingCart.getQty()) {
                shoppingCart.setQty(cart.getQty()+shoppingCart.getQty());
            }
            save = super.updateById(shoppingCart);
        }
        if (!save) {
            throw new ThrowJsonException("添加到购物车失败");
        }
        return true;
    }

	@Override
	public List<Integer> queryMoreBid(Integer cid, List<Integer> cartids) {
		return baseMapper.queryMoreBid(cid, cartids);
	}
}
